package com.resource.coursemanage.service.impl;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.resource.common.utils.R;
import com.resource.coursemanage.config.TCourseInformationListener;
import com.resource.coursemanage.entity.TCourseInformationEntity;
import com.resource.coursemanage.entity.TCourseInformationExcel;
import com.resource.coursemanage.mapper.TCourseInformationDao;
import com.resource.coursemanage.service.TCourseInformationService;

import io.renren.modules.sys.feign.StudyCourseRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.resource.common.utils.PageUtils;
import com.resource.common.utils.Query;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


@Service
public class TCourseInformationServiceImpl extends ServiceImpl<TCourseInformationDao, TCourseInformationEntity> implements TCourseInformationService {

    @Autowired
    private StudyCourseRemote studyCourseRemote;

    @Override
    public R studyCourse(TCourseInformationEntity tCourseInformation) {
        String courseAddress = tCourseInformation.getCourseAddress();
        if (StringUtils.isEmpty(courseAddress)) {
            return R.error("课程学习地址为空");
        } else {
            studyCourseRemote.alterCourseStatus(courseAddress);
        }
        return R.ok();
    }

    @Override
    public String setDate(Date date, Date startDate, Date endDate) {
        if (date.after(startDate)) {
            return "未开始";
        } else if (date.before(endDate) && date.after(startDate)) {
            return "进行中";
        }
        return "已结束";
    };

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        int size = params.size();
        IPage<TCourseInformationEntity> page=new Page<>();
        if(size==4){
            String keys="";
            String param="";
            Set<String> strings = params.keySet();
            for (String key : strings) {
                keys=key;
                param=params.get(keys).toString();
            }
            page = this.page(
                    new Query<TCourseInformationEntity>().getPage(params),
                    new QueryWrapper<TCourseInformationEntity>().eq(keys,param)
            );
            return new PageUtils(page);
        }
        page = this.page(
                new Query<TCourseInformationEntity>().getPage(params),
                new QueryWrapper<TCourseInformationEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public R exportData(HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = "courseInformation";
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            List<TCourseInformationEntity> tCourseInformationEntities = this.getBaseMapper().selectList(new QueryWrapper<TCourseInformationEntity>());
            List<TCourseInformationExcel> list = new ArrayList<>();
            int sortNo = 1;
            for (TCourseInformationEntity tCourseInformationEntity : tCourseInformationEntities) {
                TCourseInformationExcel tCourseInformationExcel = new TCourseInformationExcel();
                tCourseInformationExcel.setCourseCode(tCourseInformationEntity.getCourseCode());
                tCourseInformationExcel.setCourseName(tCourseInformationEntity.getCourseName());
                tCourseInformationExcel.setSortNo(sortNo++ + "");
                tCourseInformationExcel.setCourseContent(tCourseInformationEntity.getCourseContent());
                tCourseInformationExcel.setCourseStartTime(tCourseInformationEntity.getCourseStartTime());
                tCourseInformationExcel.setCoursePeopleAmount(tCourseInformationEntity.getCoursePeopleAmount());
                tCourseInformationExcel.setCourseTeacherCode(tCourseInformationEntity.getCourseTeacherCode());
                tCourseInformationExcel.setCourseTeacherName(tCourseInformationEntity.getCourseTeacherName());
                list.add(tCourseInformationExcel);
            }
            EasyExcel.write(response.getOutputStream(), TCourseInformationExcel.class).sheet("课程信息表").doWrite(list);
            return R.ok("导出成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("导出失败");

    }

    @Override
    public R importData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),TCourseInformationExcel.class,new TCourseInformationListener(baseMapper)).sheet("课程信息表").doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok();
    }
}